package com.juneyaoair.report;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.juneyaoair.logistics.commons.exception.BusinessException;
import com.juneyaoair.shop.enums.LogisticsEnum;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.util.*;
import java.util.regex.Pattern;

public class ExcelMain {

    /*方法二：推荐，速度最快
     * 判断是否为整数
     * @param str 传入的字符串
     * @return 是整数返回true,否则返回false
     */

    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    public static void main(String[] args) {
        List<FeightDto> list = new ArrayList<>();
       try {
            BufferedReader reader = new BufferedReader(new FileReader("e:/123/aa.csv"));//换成你的文件名
            reader.readLine();//第一行信息，为标题信息，不用,如果需要，注释掉
            String line = null;
            while((line=reader.readLine())!=null){
                String item[] = line.split("\t");//CSV格式文件为逗号分隔符文件，这里根据逗号切分
                if(isInteger(item[0])){
                    continue;
                }
                FeightDto dto=new FeightDto();
                dto.setFlightNo(item[0]);
                dto.setDepCode(item[1]);
                dto.setArrCode(item[2]);
                String[] scheduleString=item[3].split("");
                Set<String> set=new HashSet<String>();
                set.addAll(Arrays.asList(scheduleString));
                String str= StringUtils.join(set.toArray(),",");
                dto.setSchedule(str);
                dto.setFlightDeptimePlanDate(item[4]);
                dto.setFlightArrtimePlanDate(item[5]);
                dto.setAircraftType(item[6]);
                dto.setStopover(item[7]);
                dto.setIsShareFly(item[8]);
                dto.setShareFlyNo(item[9]);
                dto.setAcrossDate(item[10]);
                list.add(dto);
            }

           EasyExcel.write("e:/123/bb.xlsx", FeightDto.class).sheet("sheet名称").doWrite(list);

           /*OutputStream outputStream=new FileOutputStream(new File("e:/123/bb.xlsx"));
           try {
               ExcelWriter excelWriter = EasyExcel.write(outputStream, FeightDto.class)
                       .excelType(ExcelTypeEnum.XLSX).build();
               WriteSheet writeSheet = EasyExcel.writerSheet().build();
               excelWriter.write(list, writeSheet);
           } catch (Exception e) {

               System.out.println(e);
           } finally {


           }*/



        } catch (Exception e) {
            e.printStackTrace();
        }

}
}
